package edu.aynu.service.impl;

import edu.aynu.entity.Choice;
import edu.aynu.es.EsChoiceDao;
import edu.aynu.service.EsChoiceService;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 28241
 * @Date 2021/5/24 16:55
 */
@Service
public class EsChoiceServiceImpl implements EsChoiceService {

    @Autowired
    private EsChoiceDao esChoiceDao;

    /**
     * 通过关键字在choice索引中查询
     * @param choice
     * @return
     */
    @Override
    public List<Choice> queryBySubject(Choice choice) {
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("subject", choice.getSubject());
        List<Choice> choices = new ArrayList<>();
        esChoiceDao.search(matchQueryBuilder).forEach(choiceTemp -> choices.add(choiceTemp));
        return  choices;
    }
}
